package fl.ass.service.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import fl.ass.facade.constant.InterfaceConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.service.core.framework.biz.BaseBizImpl;
import cmm.service.core.framework.dao.IBaseDao;
import cmm.user.facade.system.api.IPriorityApiService;
import cmm.user.facade.system.api.ISysUsrApiService;
import cmm.user.facade.system.condition.SysRoleQueryCondition;
import cmm.user.facade.system.condition.SysUsrFunAuthRQueryCondition;
import cmm.user.facade.system.dto.SysRoleDTO;
import cmm.user.facade.system.dto.SysUsrDTO;
import cmm.user.facade.system.dto.SysUsrFunAuthRDTO;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.CollectionUtils;
import fl.ass.facade.condition.AssParkingMgQueryCondition;
import fl.ass.facade.dto.AssParkingMgDTO;
import fl.ass.service.dao.AssParkingMgMapper;
import fl.ass.service.po.AssParkingMg;
import fl.ass.service.service.IAssParkingMgService;
import fl.ass.service.transform.AssParkingMgTransform;

/**
 * @Description: 停车场资产管理员信息表 服务层接口实现
 * @Description: 类中已经实现了简单增删改查操作,可以直接使用,如果有需要重写该方法即可：
 * @Description: 1、add(TVO entity) 2、removeById(Long id) 3、modify(DTO entity) 4、selectById(Long id)	5、searchListPage(Pager page) 7、searchList(DTO dto)   
 * @Description: 业务层方法名称前缀：新增：add* 删除：remove* 修改：modify* 查询对象：select* get* 批量：batch* 查询集合：search*
 */
@Service
public class AssParkingMgServiceImpl extends BaseBizImpl<AssParkingMg, AssParkingMgDTO, AssParkingMgQueryCondition> implements IAssParkingMgService {
	
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(IAssParkingMgService.class);
	
	@Autowired
	private AssParkingMgMapper assParkingMgMapper;
	
	@Autowired
	private IPriorityApiService priorityApiService;//权限模块 暴露接口
	
	@Autowired
	private ISysUsrApiService sysUsrApiService;//系统用户  暴露服务
	
	@Override
	public List<AssParkingMgDTO> searchList(AssParkingMgQueryCondition condition) throws BaseException {
		if(null == condition) return new ArrayList<AssParkingMgDTO>();
		List<AssParkingMg> list = this.assParkingMgMapper.selectList(condition);
		//用户模块相关表优化-JL-20181106
		if(CollectionUtils.isNotEmpty(list) && list.size()>0){
			for(AssParkingMg po : list){
				SysUsrDTO dto = this.sysUsrApiService.selectSysUsrById(po.getManagerId(), InterfaceConstants.SYS_CODE, InterfaceConstants.ORG_CODE);
				if(null != dto){
					po.setManagerRlNm(dto.getRefRlNm());
				}
			}
		}
		return AssParkingMgTransform.toDTOList(list);
	}
	
	@Override
	public IBaseDao<AssParkingMg,AssParkingMgQueryCondition> getDao() {
		return this.assParkingMgMapper;
	}

	/**
	 * 功能：查询所有'优车资产管理员'
	 * 备注：用户模块相关表优化-JL-20181106
	 */
	@Override
	public List<AssParkingMgDTO> selectManagerList() {
		List<AssParkingMg> list = new ArrayList<AssParkingMg>();
		//1.根据角色名称'优车资产管理员'到SYS_ROLE表里查对应的角色ID
		SysRoleQueryCondition qc = new SysRoleQueryCondition();
		qc.setRoleNm("优车资产管理员");
		qc.setSysCd("11000");
		qc.setIsValid(1);
		List<SysRoleDTO> roleList = this.priorityApiService.selectSysRoleByCondition(qc);
		if(CollectionUtils.isNotEmpty(roleList) && roleList.size()>0){
			SysRoleDTO roleDto = roleList.get(0);
			if(null != roleDto && null != roleDto.getId()){
				//2.根据角色ID和系统代码查询资管员
				List<Long> roleIdList = new ArrayList<Long>();
				roleIdList.add(roleDto.getId());
				SysUsrFunAuthRQueryCondition rQc = new SysUsrFunAuthRQueryCondition();
				rQc.setRoleIdList(roleIdList);
				rQc.setSysCd("11000");
				List<SysUsrFunAuthRDTO> rDtoList = this.priorityApiService.selectRskUserList(rQc);
				if(CollectionUtils.isNotEmpty(rDtoList) && rDtoList.size()>0){
					for(SysUsrFunAuthRDTO dto : rDtoList){
						AssParkingMg mg = new AssParkingMg();
						mg.setManagerId(dto.getUsrId());
						mg.setManagerNm(dto.getUsrNm());
						mg.setManagerRlNm(dto.getRlNm());
						list.add(mg);
					}
				}
				
			}
		}
		//List<AssParkingMg> list = this.assParkingMgMapper.selectManagerList();
		return AssParkingMgTransform.toDTOList(list);
	}

    @Override
    public Long getRecentParkMg(Long parkId) throws BaseException {
        if (StringUtils.isEmpty(parkId)) {
            throw new BaseException("未查到拖车停车场ID，请联系管理员处理");
        }
	    try {
            AssParkingMgQueryCondition assParkingMgQueryCondition = new AssParkingMgQueryCondition();
            assParkingMgQueryCondition.setParkId(parkId);
            List<AssParkingMgDTO> assParkingMgDTOS = this.searchList(assParkingMgQueryCondition);
            if (CollectionUtils.isNotEmpty(assParkingMgDTOS)) {
                return assParkingMgDTOS.get(0).getManagerId();
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("根据停车场id查找资产管理员失败").setKeys("parkId:" + parkId).setThrowable(e).setWarning(false).error(LogExceptionEnum.COMMON_CODE_0002);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("根据停车场id查找资产管理员失败").setKeys("parkId:" + parkId).setThrowable(e).setWarning(false).error(LogExceptionEnum.API_CODE_0102);
            throw e;
        }
        return null;
    }
}

